package com.thti.mecool.client.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.Toolbar;

public class SaleBrandAndCategoryActivity extends BaseActivity {
	private ListView categoryList;
	private TextView storeNameTextView;
	private ArrayList<String> NameList;
	private ArrayList<EditText> EditList;
	private Button save;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.mecool_single_list);
		initToolbar();
		initData();
		storeNameTextView=(TextView)findViewById(R.id.mecool_single_list_storename);
		storeNameTextView.setText(getIntent().getStringExtra("titlename"));
		categoryList = (ListView) findViewById(R.id.mecool_single_list_listview);
		categoryList.setAdapter(categoryAdapter);
	}
	
	@SuppressWarnings("unchecked")
	private void initData() {
		NameList = (ArrayList<String>) getIntent()
				.getSerializableExtra("NameList");
		save=(Button)findViewById(R.id.mecool_single_list_save);
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Save();
			}
		});
		EditList=new ArrayList<EditText>();
	}

	private BaseAdapter categoryAdapter = new BaseAdapter() {
		@SuppressWarnings("unused")
		private Integer index = -1;

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder holder = null;
			if (arg1 == null) {
				holder = new ViewHolder();
				arg1 = getLayoutInflater().inflate(
						R.layout.mecool_sale_category_item, null);
				holder.id = (TextView) arg1
						.findViewById(R.id.mecool_sale_category_id);
				holder.item = (TextView) arg1
						.findViewById(R.id.mecool_sale_category_item);
				holder.quantity=(EditText)arg1.findViewById(R.id.mecool_sale_category_quantity);
				
				EditList.add(holder.quantity);
				
				holder.id.setText(arg0 + 1 + "");
				holder.item.setText(NameList.get(arg0));
				holder.quantity.setTag(arg0);
				OnTouchListener onTouchListener=new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_UP) {
							index = (Integer) v.getTag();
						}
						return false;
					}
				};
				holder.quantity.setOnTouchListener(onTouchListener);
			}
			return arg1;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return NameList.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return NameList.size();
		}
		class ViewHolder {
			TextView id, item;
			EditText quantity;
		}
	};
	
	private void Save(){
		for(EditText et:EditList){
			System.out.println(et.getText().toString());
			Util.sendToast(this, "结果已保存");
			finish();
		}
	}
	
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText(getIntent().getStringExtra("uploadtitle"));
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
	}};
}
